package xy.donate.android.ui.activity

import android.annotation.SuppressLint
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.room.Room
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import kotlinx.android.synthetic.main.activity_record_detail.*
import xy.donate.android.R
import xy.donate.android.common.MyActivity
import xy.donate.android.constant.Constant
import xy.donate.android.db.HandsUpDB
import xy.donate.android.db.HandsUpDao

class RecordDetailActivity : MyActivity() {

    private lateinit var db: HandsUpDao

    override fun getLayoutId(): Int {
        return R.layout.activity_record_detail
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {

        db = Room.databaseBuilder(this, HandsUpDB::class.java, Constant.RECORD_TABLE)
                .allowMainThreadQueries()
                .build().handsDao

        val data = db.getDetail(intent.getStringExtra("recordID"))

        val adapter: BaseQuickAdapter<String, BaseViewHolder>
        recycler_view.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        recycler_view.setHasFixedSize(true)
        adapter = object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_help_to_img, data.images) {
            override fun convert(holder: BaseViewHolder, item: String) {
                holder.setGone(R.id.iv_delete,true)
                Glide.with(context.applicationContext)
                        .load(item)
                        .into(holder.getView(R.id.iv_item_img))
            }
        }
        adapter.addChildClickViewIds(R.id.iv_item_img, R.id.iv_delete)
        adapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.iv_item_img -> {
                    ImagePreviewActivity.start(this, adapter.data)
                }
                R.id.iv_delete -> {
                    adapter.removeAt(position)
                }
            }
        }
        recycler_view.adapter = adapter

        tv_name_phone.text = data.name
        tv_order_address.text = data.phone
        tv_order_no.text = "申请编号${data.recordID}"
        tv_order_time.text = "申请时间${data.recordTime}"
        tv_order_remark.text = data.hardText
        tv_help_text.text = data.helpText

        ll_commit_order.setOnClickListener {
            finish()
        }
    }

    override fun initData() {

    }
}